package com.travelbid.admin.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.ActionSupport;

import com.travelbid.admin.pojo.TUser;
import com.travelbid.admin.service.AdminUserService;
import com.travelbid.admin.struts.form.CreateUserForm;


public class CreateUserAction extends ActionSupport {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		Integer userId = (Integer) request.getSession().getAttribute("USER_ID");
		if (userId != null) {
			try {
				CreateUserForm creatUserForm = (CreateUserForm) form;
				String username = creatUserForm.getUsername();
				String pwd = creatUserForm.getPwd();
				String nickname = creatUserForm.getNickname();
				String contactPerson = creatUserForm.getContactPerson();
				String telphone = creatUserForm.getTelphone();
				String email = creatUserForm.getEmail();
				Integer type = new Integer(creatUserForm.getType());
				String sex = creatUserForm.getSex();
				String fax = creatUserForm.getFax();
				String msn = creatUserForm.getMsn();
				String qq = creatUserForm.getQq();
				String companyName = creatUserForm.getCompanyName();
				String companyAddress = creatUserForm.getCompanyAddress();			
				
				TUser user = new TUser();
				user.setUsername(username);
				user.setPwd(pwd);
				user.setNickname(nickname);
				user.setContactPerson(contactPerson);
				user.setTelphone(telphone);
				user.setEmail(email);
				user.setType(type);
				user.setSex(sex);
				user.setFax(fax);
				user.setMsn(msn);
				user.setQq(qq);
				user.setCompanyName(companyName);
				user.setCompanyAddress(companyAddress);
				AdminUserService service = (AdminUserService)getWebApplicationContext().getBean("AdminUserService");
				
				service.AddNewUser(user);
				
				creatUserForm.reset(mapping, request);
				Integer userid = user.getId();
//				if (type.intValue() == 1) {
//					for (int i = 0; i < creatUserForm.getInner_catvalue().length; i++) {
//						String catvalue = creatUserForm.getInner_catvalue()[i];
//						Integer catid = new Integer(catvalue.split("_")[0]);
//						Integer valuetype = new Integer(catvalue.split("_")[1]);
//						if (!service.existUserCat(userid, catid, valuetype))
//							service.saveUserCat(userid, catid, valuetype);
//					}
//				} else if (type.intValue() == 2) {
//					for (int i = 0; i < creatUserForm.getOuter_catvalue().length; i++) {
//						String catvalue = creatUserForm.getOuter_catvalue()[i];
//						Integer catid = new Integer(catvalue);
//						Integer valuetype = new Integer(0);
//						if (!service.existUserCat(userid, catid, valuetype))
//							service.saveUserCat(userid, catid, valuetype);
//					}
//				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return mapping.findForward("createUserSec");
		} else {
			return mapping.findForward("NotLogin");
		}
	}

}
